home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 25.1 KB | 862 lines |
- package symantec.itools.awt;
-
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.awt.Toolkit;
- import java.net.URL;
- import java.net.MalformedURLException;
- import java.awt.Event;
- import java.awt.Container;
- import java.applet.*;
- import java.awt.Canvas;
- import symantec.itools.awt.TransparencyTrick;
- import symantec.itools.awt.TransparencyTrickUtils;
-
- /**
- * This is a button that allows three different states. The standard state
- * is when the button is not doing anything. Durring this state you can specify
- * null for the standard image, and the standard state will be transparent. The
- * over state is when the mouse cursor is over the button. This state also displays
- * the LinkURL if it is not null. The down state is when the mouse button is pressed
- * down inside the button. If there is a click in the button, it will attempt to go
- * to the LinkURL, unless it is null. This state also displays the LinkURL if it is
- * not null.
- * @version 1.0, Feb 4, 1997
- * @author Symantec
- */
-
- // Created and implemented by Levi Brown, Symantec Macintosh Internet Tools.
- // 02/04/97 LAB Checked it in
-
- public class RollOverButton extends java.awt.Canvas implements TransparencyTrick
- {
- /**
- * The image displayed when the button is not doing anything.
- */
- protected Image StandardImage;
- /**
- * The image displayed when the mouse is over the button.
- */
- protected Image OverImage;
- /**
- * The image displayed when the mouse is pressed down inside the button.
- */
- protected Image DownImage;
- /**
- * The file name of the image to use in default cases.
- */
- protected String StandardFileName;
- /**
- * The filename for the image to use when the mouse is over the button.
- */
- protected String OverFileName;
- /**
- * The filename for the image to use when the mouse is over the button and depressed.
- */
- protected String DownFileName;
- /**
- * Frame specifier for showing a URL document in a browser or applet
- * viewer. It is interpreted as follows:
- * <UL>
- * <DT>"_self" show document in the current frame</DT>
- * <DT>"_parent" show document in the parent frame</DT>
- * <DT>"_top" show document in the topmost frame</DT>
- * <DT>"_blank" show document in a new unnamed toplevel window</DT>
- * <DT>all others show document in a new toplevel window with the given name</DT>
- * </UL>
- */
- protected String frame;
- /**
- * The URL for the image to use in defaullt cases.
- */
- protected URL StandardURL;
- /**
- * The URL for the image to use when the mouse is over the button.
- */
- protected URL OverURL;
- /**
- * The URL for the image to use when the mouse is over the button and depressed.
- */
- protected URL DownURL;
- /**
- * The URL that determines the link the button will go to on mouse up
- */
- protected URL LinkURL;
- /**
- * The flag indicating the image should be centered in the bounds of the
- * button. If true the image is centered. If false the image is drawn at
- * 0,0 relative to the bounds of the button.
- */
- protected boolean isCenterMode;
- /**
- * Indicates the mouse is over the button.
- */
- protected boolean isMouseOver;
- /**
- * Indicates the frame should be cleared in between drawing
- * different button states.
- */
- protected boolean isClearFrame;
- /**
- * Indicates the mouse button is being pressed.
- */
- protected boolean isPressed;
- /**
- * Indicates the mouse is being dragged in the button.
- */
- protected boolean isMouseDrag;
- /**
- * The context used to display the document.
- */
- protected AppletContext context;
-
- /**
- * Constructs a RollOverButton.
- */
- public RollOverButton()
- {
- StandardImage = null;
- OverImage = null;
- DownImage = null;
- StandardFileName = null;
- OverFileName = null;
- DownFileName = null;
- frame = null;
- StandardURL = null;
- OverURL = null;
- DownURL = null;
- LinkURL = null;
- isMouseOver = false;
- isClearFrame = false;
- isPressed = false;
- isMouseDrag = false;
- }
-
- /**
- * Constructor allowing file name specifications for all three sates of the button.
- * @param Standard the filename for the image to use in defaullt cases
- * @param Over the filename for the image to use when the mouse is over the button
- * @param Down the filename for the image to use when the mouse is over the button and depressed
- */
- public RollOverButton(String Standard, String Over, String Down)
- throws MalformedURLException
- {
- this();
- setStandardFileName(Standard);
- setOverFileName(Over);
- setDownFileName(Down);
- }
-
- /**
- * Constructor allowing URL specifications for all three sates of the button.
- * @param Standard the URL for the image to use in defaullt cases
- * @param Over the URL for the image to use when the mouse is over the button
- * @param Down the URL for the image to use when the mouse is over the button and depressed
- */
- public RollOverButton(URL Standard, URL Over, URL Down)
- {
- this();
- setStandardURL(Standard);
- setOverURL(Over);
- setDownURL(Down);
- }
-
- /**
- * Constructor allowing image specifications for all three sates of the button.
- * @param Standard the image to use in defaullt cases
- * @param Over the image to use when the mouse is over the button
- * @param Down the image to use when the mouse is over the button and depressed
- */
- public RollOverButton(Image Standard, Image Over, Image Down)
- {
- this();
- setStandardImage(Standard);
- setOverImage(Over);
- setDownImage(Down);
- }
-
- /**
- * Sets the file name of the image to use in default cases.
- * @param str the file name to use for default cases
- * @see #getStandardFileName
- */
- public void setStandardFileName(String str)
- throws MalformedURLException
- {
- StandardFileName = str;
- setFileNameHelper(StandardFileName);
- }
-
- /**
- * Gets the file name of the image to use in default cases.
- * @return the file name being used for default cases
- * @see #setStandardFileName
- */
- public String getStandardFileName()
- {
- return StandardFileName;
- }
-
- /**
- * Sets the file name of the image to use when the mouse is over the button.
- * @param str the file name to use when the mouse is over the button
- * @see #getOverFileName
- */
- public void setOverFileName(String str)
- throws MalformedURLException
- {
- OverFileName = str;
- setFileNameHelper(OverFileName);
- }
-
- /**
- * Gets the file name of the image to use when the mouse is over the button.
- * @return str the file name used when the mouse is over the button
- * @see #setOverFileName
- */
- public String getOverFileName()
- {
- return OverFileName;
- }
-
- /**
- * Sets the file name of the image to use when the mouse is down while in the button.
- * @param str the file name to use when the mouse is down while in the button
- * @see #getDownFileName
- */
- public void setDownFileName(String str)
- throws MalformedURLException
- {
- DownFileName = str;
- setFileNameHelper(DownFileName);
- }
-
- /**
- * Gets the file name of the image to use when the mouse is down while in the button.
- * @return str the file name used when the mouse is down while in the button
- * @see #setDownFileName
- */
- public String getDownFileName()
- {
- return DownFileName;
- }
-
- /**
- * A function used to consolidate shared code between the three file name setting functions.
- * @param str the file name to set
- */
- protected void setFileNameHelper(String str)
- throws MalformedURLException
- {
- try
- {
- setOverURL(new URL(str));
- }
- catch(MalformedURLException e)
- {
- //System.out.println("malformed URL");
- }
- repaint();
- }
-
- /**
- * Sets the URL of the image to use when in default states.
- * @param aUrl the URL to use when in default states
- * @see #getStandardURL
- */
- public void setStandardURL(URL aUrl)
- {
- StandardURL = aUrl;
- StandardFileName = null;
-
- Image loadedImage = getToolkit().getImage(aUrl);
- if (loadedImage != null) {
- setStandardImage(loadedImage);
- repaint();
- }
- }
-
- /**
- * Gets the URL of the image used when in default states.
- * @return the URL used when in default states
- * @see #setStandardURL
- */
- public URL getStandardURL()
- {
- return StandardURL;
- }
-
- /**
- * Sets the URL of the image to use when the mouse is over the button.
- * @param aUrl the URL to use when the mouse is over the button
- * @see #getOverURL
- */
- public void setOverURL(URL aUrl)
- {
- OverURL = aUrl;
- OverFileName = null;
-
- Image loadedImage = getToolkit().getImage(aUrl);
- if (loadedImage != null) {
- setOverImage(loadedImage);
- repaint();
- }
- }
-
- /**
- * Gets the URL of the image used when the mouse is over the button.
- * @return the URL used when the mouse is over the button
- * @see #setOverURL
- */
- public URL getOverURL()
- {
- return OverURL;
- }
-
- /**
- * Sets the URL of the image to use when the mouse is down while in the button.
- * @param aUrl the URL to use when the mouse is down while in the button
- * @see #getDownURL
- */
- public void setDownURL(URL aUrl)
- {
- DownURL = aUrl;
- DownFileName = null;
-
- Image loadedImage = getToolkit().getImage(aUrl);
- if (loadedImage != null) {
- setDownImage(loadedImage);
- repaint();
- }
- }
-
- /**
- * Gets the URL of the image used when the mouse is down while in the button.
- * @return the URL used when the mouse is down while in the button
- * @see #setDownURL
- */
- public URL getDownURL()
- {
- return DownURL;
- }
-
- /**
- * Sets the image to use when in default states.
- * @param img the Image to use when in default states
- * @see #getStandardImage
- */
- public void setStandardImage(Image img)
- {
- StandardFileName = null;
- StandardImage = img;
- setImageHelper(StandardImage);
- }
-
- /**
- * Gets the URL of the image used when in default states.
- * @return the Image used when the in default states
- * @see #setStandardImage
- */
- public Image getStandardImage()
- {
- return StandardImage;
- }
-
- /**
- * Sets the image to use when the mouse is over the button.
- * @param img the Image to use when the mouse is over the button
- * @see #getOverImage
- */
- public void setOverImage(Image img)
- {
- OverFileName = null;
- OverImage = img;
- setImageHelper(OverImage);
- }
-
- /**
- * Gets the URL of the image used when the mouse is over the button.
- * @return the Image used when the mouse is over the button
- * @see #setOverImage
- */
- public Image getOverImage()
- {
- return OverImage;
- }
-
- /**
- * Sets the image to use when the mouse is down while in the button.
- * @param img the Image to use when the mouse is down while in the button
- * @see #getDownImage
- */
- public void setDownImage(Image img)
- {
- DownFileName = null;
- DownImage = img;
- setImageHelper(DownImage);
- }
-
- /**
- * Gets the URL of the image used when the mouse is down while in the button.
- * @return the Image used when the mouse is down while in the button
- * @see #setDownImage
- */
- public Image getDownImage()
- {
- return DownImage;
- }
-
- /**
- * A function used to consolidate shared code between the three image setting functions.
- * @param img the image to set
- */
- protected void setImageHelper(Image img)
- {
- if (img != null)
- {
- MediaTracker tracker;
-
- try
- {
- tracker = new MediaTracker(this);
- tracker.addImage(img, 0);
- tracker.waitForID(0);
- }
- catch(InterruptedException e){}
- }
- else
- {
- repaint();
- }
- }
-
- /**
- * Sets the flag to erase before drawing a button state.
- * @param b if true then clear the frame between the different states of the button
- * if false then don't clear the frame
- * @see #getClearFrame
- */
- public void setClearFrame(boolean b)
- {
- isClearFrame = b;
- }
-
- /**
- * Gets the flag that determines erasing before drawing a button state.
- * @return if true then the frame will be cleared between the different states of the button,
- * if false then the frame will not be cleared
- * @see #setClearFrame
- */
- public boolean getClearFrame()
- {
- return isClearFrame;
- }
-
- /**
- * Sets the URL that determines the link the button will go to on mouse up.
- * @param u the URL that determines the link the button will go to on mouse up
- * @see #getURL
- */
- public void setURL(URL u)
- {
- LinkURL = u;
- context = null;
- }
-
- /**
- * Gets the URL that determines the link the button will go to on mouse up.
- * @return the URL that determines the link the button will go to on mouse up
- * @see #setURL
- */
- public URL getURL()
- {
- return LinkURL;
- }
-
-
- /**
- * Sets the document frame type/name specifier.
- * @param f the new frame type/name specifier
- * @see #getFrame
- * @see #frame
- */
- public void setFrame(String f)
- {
- frame = f;
- }
-
- /**
- * Gets the document frame type/name specifier.
- * @return the current frame type/name specifier
- * @see #setFrame
- * @see #frame
- */
- public String getFrame()
- {
- return frame;
- }
-
- /**
- * Sets the flag to center the image in the bounds of the button.
- * @param b the new center mode. If true then center the image in the
- * bounds of the button, if false then draw the image at 0,0 relative to
- * the bounds of the button
- * @see #getCenterMode
- */
- public void setCenterMode(boolean flag)
- {
- isCenterMode = flag;
- invalidate();
- }
-
- /**
- * Gets the flag to center the image in the bounds of the button.
- * @return the current center mode. If true then the image will be
- * centered in the bounds of the button, if false then the image will
- * be drawn at 0,0 relative to the bounds of the button.
- * @see #setCenterMode
- */
- public boolean getCenterMode()
- {
- return isCenterMode;
- }
-
- /**
- * Sets the context used to view documents.
- * @param c the new applet context
- */
- protected void setAppletContext(AppletContext c)
- {
- context = c;
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see #update
- */
- public void paint(Graphics g)
- {
- Dimension dim = size();
-
- int x, y;
- x = y = 0;
-
- if(isPressed && isMouseOver)
- {
- //Mouse Down Image
- if(DownImage != null)
- {
- if (isCenterMode)
- {
- x += (dim.width - DownImage.getWidth(this)) / 2;
- y += (dim.height - DownImage.getHeight(this)) / 2;
- }
- g.drawImage(DownImage, x, y, this);
- }
- }
- else
- {
- //RollOver Image
- if(isMouseOver && OverImage != null)
- {
- if (isCenterMode)
- {
- x += (dim.width - OverImage.getWidth(this)) / 2;
- y += (dim.height - OverImage.getHeight(this)) / 2;
- }
- g.drawImage(OverImage, x, y, this);
- }
- //Standard Image
- else
- {
- if (StandardImage == null)
- {
- //Draw the standard state as "Transparent"
- TransparencyTrickUtils.paintComponentsBehind(this, g);
- }
- else
- {
- if (isCenterMode)
- {
- x += (dim.width - StandardImage.getWidth(this)) / 2;
- y += (dim.height - StandardImage.getHeight(this)) / 2;
- }
- g.drawImage(StandardImage, x, y, this);
- }
- }
- }
- }
-
- /**
- * Processes MOUSE_ENTER events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_ENTER
- * event. These events occur when the mouse first moves over this
- * component.
- * It keeps track of the mouse position relative to the button, and displays the URL Link.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseExit
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseEnter(Event e, int x, int y)
- {
- isMouseOver = true;
- //Display the LinkURL
- if (context != null && LinkURL != null)
- {
- context.showStatus(LinkURL.toString());
- }
- //Draw the button
- repaint();
-
- return true;
- }
-
- /**
- * Processes MOUSE_EXIT events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_EXIT
- * event. These events occur when the mouse first leaves this
- * component.
- * It keeps track of the mouse position relative to the button,
- * and erases the URL Link.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseEnter
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseExit(Event e, int x, int y)
- {
- isMouseOver = false;
- if (context != null && LinkURL != null)
- {
- context.showStatus("");
- }
- //Draw the button in the standard state
- repaint();
-
- return true;
- }
-
- /**
- * Processes MOUSE_DOWN events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_DOWN
- * event. These events occur when the mouse button is pressed while
- * inside this component.
- * It handles the mouse down event by setting the boolean isPressed.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseUp
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseDown(Event e, int x, int y)
- {
- isPressed = true;
- //Draw the button
- repaint();
-
- return true;
- }
-
- /**
- * Processes MOUSE_UP events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_UP
- * event. These events occur when the mouse button is released while
- * inside this component.
- * It handles the mouse up event by linking to the LinkURL if it is not null, sets the
- * isPressed boolean, and posts an Action Event.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseDown
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseUp(Event e, int x, int y)
- {
- if (isPressed)
- {
- isPressed = false;
- //Draw the button in the over state.
- repaint();
- //Handle going to the LinkURL
- if (context != null & LinkURL != null)
- {
- if (frame == null || frame.length() == 0)
- context.showDocument(LinkURL);
- else
- context.showDocument(LinkURL, frame);
- }
- postEvent(new Event(this, Event.ACTION_EVENT, null));
- }
-
- return true;
- }
-
- /**
- * Processes MOUSE_DRAG events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_DRAG
- * event. These events occur when the mouse is moved around inside this
- * component while the button is pressed.
- * It keeps track of the state of the mouse button while the mouse
- * position is in the button.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseMove
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseDrag(Event e, int x, int y)
- {
- isMouseDrag = true;
- return true;
- }
-
- /**
- * Processes MOUSE_MOVE events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_MOVE
- * event. These events occur when the mouse is moved around inside this
- * component while the button is NOT pressed.
- * It keeps track of the state of the mouse button while the mouse
- * position is in the button.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseDrag
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseMove(Event e, int x, int y)
- {
- isMouseDrag = false;
- isPressed = false;
- return true;
- }
-
- /**
- * Handles redrawing of this component on the screen.
- * This is a standard Java AWT method which gets called by the Java
- * AWT (repaint()) to handle repainting this component on the screen.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its <0,0> coordinate is this component's
- * top-left corner.
- * Typically this method paints the background color to clear the
- * component's drawing space, sets graphics context to be the foreground
- * color, and then calls paint() to draw the component.
- *
- * It is overridden here to add the clear frame feature to this button.
- *
- * @param g the graphics context
- * @see java.awt.Component#repaint
- * @see #paint
- */
- public void update(Graphics g)
- {
- if (isClearFrame)
- super.update(g);
- else
- paint(g);
- }
-
- /**
- * Ensures that this component is laid out properly, as needed.
- * This is a standard Java AWT method which gets called by the AWT to
- * make sure this component and its subcomponents have a valid layout.
- * If this component was made invalid with a call to invalidate(), then
- * it is laid out again.
- *
- * It is overridden here to locate the applet containing this component.
- *
- * @see java.awt.Component#invalidate
- */
- public void validate()
- {
- // On validation, try to find the containing applet. If we can find
- // it, we don't bother doing the link...
- Container c;
-
- c = getParent();
-
- while (c != null)
- {
- if (c instanceof Applet)
- {
- setAppletContext(((Applet) c).getAppletContext());
- break;
- }
-
- c = c.getParent();
- }
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- * In this case the recommended size is the minimum dimension needed to
- * encompass the three images that might be displayed.
- *
- * @see java.awt.Component#minimumSize
- */
- public Dimension preferredSize()
- {
- int iWidth, oWidth, dWidth, maxWidth, iHeight, oHeight, dHeight, maxHeight;
- iWidth = StandardImage == null ? 0 : StandardImage.getWidth(this);
- oWidth = OverImage == null ? 0 : OverImage.getWidth(this);
- dWidth = DownImage == null ? 0 : DownImage.getWidth(this);
- iHeight = StandardImage == null ? 0 : StandardImage.getHeight(this);
- oHeight = OverImage == null ? 0 : OverImage.getHeight(this);
- dHeight = DownImage == null ? 0 : DownImage.getHeight(this);
-
- maxWidth = iWidth >= oWidth ? iWidth : oWidth;
- maxWidth = maxWidth >= dWidth ? maxWidth : dWidth;
- maxHeight = iHeight >= oHeight ? iHeight : oHeight;
- maxHeight = maxHeight >= dHeight ? maxHeight : dHeight;
-
- maxWidth = maxWidth == 0 ? 10 : maxWidth;
- maxHeight = maxHeight == 0 ? 10 : maxHeight;
-
- return (new Dimension(maxWidth, maxHeight));
- }
- }
-